package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.service.CartService;

import com.changgou.order.utils.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private TokenDecode tokenDecode;

    @GetMapping("/add")
    public Result add(@RequestParam("skuId") String skuId, @RequestParam("num") Integer num) {
        cartService.add(skuId, num, this.getUsername());
        return new Result(true, StatusCode.OK, "添加购物车成功");
    }

    @GetMapping("/list")
    public Map list() {

        Map map = cartService.list(this.getUsername());
        map.put("username",this.getUsername());
        return map;
    }

    /**
     * 通过工具类获取用户名
     *
     * @return
     */
    public String getUsername() {
        String username = tokenDecode.getUserInfo().get("username");
        return username;
    }
}
